import { post, get, ResultBean } from '@/utils/request';
import type { UserState } from '@/store/modules/user/types';

export interface LoginData {
  userName: string;
  policeNum: string;
}

export interface LoginRes {
  token: string;
  roleId: number;
  userId: number;
  userName: string;
  groupId: number;
  groupName: string;
}

// 用户信息接口定义
export interface UserInfo {
  userId?: number;
  name: string;
  iphone: string;
  role: number;
  policeNum: string;
  group: number;
  dept: number;
  createTime?: number;
  updateTime?: number;
}

// 用户查询参数
export interface UserQueryParams {
  page?: number;
  pageNo?: number;
  pageSize?: number;
  name?: string;
  policeNum?: string;
  role?: number;
  group?: number;
  dept?: number;
}

// 用户列表响应
export interface UserListResponse {
  rows: UserInfo[];
  page: {
    pageNo: number;
    pageSize: number;
    totalRow: number;
  };
}

// 科室信息接口
export interface DeptInfo {
  id: number;
  name: string;
}

// 组织架构信息接口
export interface OrgInfo {
  groupId: number;
  groupName: string;
  upGroupId: number | null;
  children?: OrgInfo[];
}

export function login(data: LoginData) {
  return post<ResultBean<LoginRes>>('/api/login', data);
}

export function logout() {
  return get('/api/logout');
}

export function getUserInfo() {
  return post<UserState>('/api/user/info');
}

// 获取验证码
export function getCaptcha(data: { tel: string }) {
  return post<{ captcha?: string }>('/api/login/captcha', data);
}

// 用户管理API
export function getUserList(params: UserQueryParams) {
  return post<ResultBean<UserListResponse>>('/api/user/query', params);
}

export function getUserById(userId: number) {
  return get<ResultBean<UserInfo>>(`/api/user/${userId}`);
}

export function createUser(data: UserInfo) {
  return post<ResultBean<UserInfo>>('/api/user/save', data);
}

export function updateUser(data: UserInfo) {
  return post<ResultBean<UserInfo>>(`/api/user/upd`, data);
}

export function deleteUser(userId: number) {
  return post<ResultBean<void>>(`/api/user/del`, userId);
}

export function batchDeleteUsers(userIds: number[]) {
  return post<ResultBean<null>>('/api/user/batch-delete', { userIds });
}

// 获取科室列表
export function getDeptList() {
  return get<ResultBean<DeptInfo[]>>('/api/dept/list');
}

// 获取组织架构列表
export function getOrgList() {
  return get<ResultBean<OrgInfo[]>>('/api/org/list');
}

// 新增：获取整改人员（按任务分组）
export function queryUserByTask() {
  return get<ResultBean<any>>('/api/user/queryByTask');
}

// 新增：获取审核人（按任务分组）
export function queryAuditorByTask() {
  return get<ResultBean<any>>('/api/user/queryByReview');
}

// 验证警号是否重复
export function validUserByPoliceNum(policeNum: string, userId?: number) {
  const params: any = { policeNum };
  if (userId) {
    params.userId = userId;
  }
  return get<ResultBean<{ exists: boolean }>>('/api/user/validUserByPoliceNum', params);
}

export function getUserNameByIds(userIds: number[]) {
  return post<ResultBean<string[]>>('/api/user/getUserNameByIds', userIds);
}

// ===== Dashboard / Workplace requests (抽离自 index.vue) =====

// 我发起的 - 分页查询
export interface QueryWorkPageParams {
  serviceItemTitle?: string;
  processStatus?: string;
  instanceId?: string;
  pageNo?: number;
  pageSize?: number;
}

export interface PageInfo {
  pageNo: number;
  pageSize: number;
  totalRow: number;
}

export interface QueryWorkPageResp<T = any> {
  rows: T[];
  page: PageInfo;
}

export function queryWorkPage(params: QueryWorkPageParams) {
  return post<ResultBean<QueryWorkPageResp>>('/node/processrun/queryWorkPage', params);
}

// 个人待办查询
export interface QueryMyTaskParams {
  pageNo: number;
  pageSize: number;
  search?: string;
  searchType?: string;
  serviceWorkOrderNumber?: string;
  serviceWorkOrderNumberList?: any[];
  tenantId?: number;
  userId?: string;
  runExecutionId?: string;
}

export interface QueryMyTaskResp<T = any> {
  rows: T[];
}

export function queryMyTask(params: QueryMyTaskParams) {
  return post<ResultBean<QueryMyTaskResp>>('/node/agencyTasks/queryMyTask', params);
}

// 保存不限读字段值
export type SaveUnlimitedValueItem = {
  columnKey: string;
  instanceId: string | number;
  taskId: string | number;
  value: any;
  viewId: string | number;
};

export type SaveUnlimitedValueBody = SaveUnlimitedValueItem[];

export function saveUnlimitedValue(data: SaveUnlimitedValueBody) {
  return post<ResultBean<void>>('/node/processrun/saveUnlimitedValue', data);
}
